<?php
namespace app\admin\controller;
use think\view;
use think\Db;
use think\Controller;
class Msg extends CommonController
{
    public function _initialize()
    {
        $this->sql =  Db::name('msg');
    }
    //留言管理
    public function index()
    {
        if($_GET['search']){
            $search =  $_GET['search'];
            if($search==null){
                $list = '';
            }else{

                $list = $this->sql
                    ->where('title','like','%'.$search.'%')
                    ->select();
                $this->assign('list',$list);
            }
        }else{
            $count = $this->sql ->count(); //计算记录数
            $limitRows = 5; // 设置每页记录数
            $p = new AjaxPage($count, $limitRows,"news"); //第三个参数是你需要调用换页的ajax函数名

            $limit_value = $p->firstRow . "," . $p->listRows;
            $list = $this->sql->order('uptime desc')->limit($limit_value)->select();
            $page = $p->show(); // 产生分页信息，AJAX的连接在此处生成

            $this->assign('list',$list);

            $this->assign('page',$page);
        }

            if(!empty($res['p'])){
                $p = $res['p'];
                $this->assign('p',$p);
            }

        return $this->fetch();
    }
   //删除留言
    public  function del()
    {
        if ($_POST) {
            $map['id'] = $_POST['id'];
            $res = $this->sql ->where($map)->delete();
            if ($res) {
                return show(1, '删除成功！');
            } else {
                return show(0, '删除失败！');
            }
        }
    }

//编辑留言
    public function edit()
    {
        if($_GET){
            $map['id']  = $_GET['id'];
            $list = $this->sql->where($map)->find();
            $this->assign('list',$list);
        }
        if($_POST){
                $id  = $_POST['id'];
                $_POST['uptime'] = date('Y-m-d H:i:s',time());
                $add = $this->sql->where(array(
                    'id'=>$id
                ))->update($_POST);
                if($add){
                    $this->success('修改成功', url('Msg/index'));

                }else{
                    $this->error('修改失败');
                }
            }

        return $this->fetch();

        }

}
